<?php

/**
 * Entity form.
 *
 * @package    Redsea20
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EntityForm extends BaseEntityForm
{
    protected static $genders = array();
    protected static $entityTypes = array();
    
    public function configure()
    {
        $this->disableCSRFProtection();

        /*
         * By default, the year select tag (of sfWidgetFormDate) is populated
         * with the 10 years around the current year. This can be changed by
         * using the years option:
         */
        $years = range(1930, 2011);

        // Setting genders
        $genders = array('Male' => 'Male', 'Female' => 'Female');
        
        //Swapping the KEYS of ENT_TPYE array with its VALUES
        foreach(Types::$ENT_TYPE as $key => $value)
        {
            $entityTypes[$value] = $key;
        }

      $this->setWidgets(array(
      'id'              => new sfWidgetFormInputHidden(),
      'firstname'       => new sfWidgetFormInputText(),
      'lastname'        => new sfWidgetFormInputText(),
      'position'        => new sfWidgetFormInputText(),
      'university'      => new sfWidgetFormInputText(),
      'address'         => new sfWidgetFormInputText(),
      'contact'         => new sfWidgetFormInputText(),
      'picture'         => new sfWidgetFormInputFile(),
      'birthdate'       => new sfWidgetFormInputText(),
      //'entitytyperefid' => new sfWidgetFormInputHidden(),
      'entitytyperefid' => new sfWidgetFormSelect(array('choices' => $entityTypes)),
      'gender'          => new sfWidgetFormSelect(array('choices' => $genders)),
      'emailaddress'    => new sfWidgetFormInputText(),
    ));

      // There are two types of user groups 1) Professor and 2) General User
      // so setting General User as default
      //$this->widgetSchema['entitytyperefid']->setDefault(7);
      $this->widgetSchema['entitytyperefid']->setOption('default', 9);
        

      // Setting the validators over form fields
      $this->setValidators(array(
      'id'              => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'firstname'       => new sfValidatorString(array('max_length' => 50)),
      'lastname'        => new sfValidatorString(array('max_length' => 50)),
      'emailaddress'    => new sfValidatorEmail(array('required' => true)),
      'picture'         => new sfValidatorFile(array('required' => false)),
      'birthdate'       => new sfValidatorDate(array('required' => false)),
      'gender'          => new sfValidatorString(array('required' => FALSE)),
      'contact'          => new sfValidatorString(array('required' => FALSE)),
      'address'          => new sfValidatorString(array('required' => FALSE)),
      'university'        => new sfValidatorString(array('required' => FALSE)),
      'position'          => new sfValidatorString(array('required' => FALSE)),
      //'entitytyperefid' => new sfValidatorInteger(),
    ));

      $this->validatorSchema['firstname']->setOption('min_length', 3);
      $this->validatorSchema['lastname']->setOption('min_length', 3);
      $this->validatorSchema['emailaddress']->setMessage('invalid', 'Please enter a valid email address');

      // Adding extra fields/validators for password and confirm password
      $this->widgetSchema['password'] = new sfWidgetFormInputPassword();
      $this->widgetSchema['password_check'] = new sfWidgetFormInputPassword();
      
      $this->validatorSchema['password'] = new sfValidatorString(array('required' => true, 'min_length' => 8, 'max_length' => 50));
      $this->validatorSchema['password_check'] = new sfValidatorString(array('required' => true, 'min_length' => 8, 'max_length' => 50));
      $this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_check', array(), array('invalid' => 'Password Mismatch')));

      // Setting error message for Password (Minimum Length Error)
      $this->validatorSchema['password']->setMessage('min_length', 'Password is too short (8 characters min)');
      $this->validatorSchema['password_check']->setMessage('min_length', 'Password is too short (8 characters min)');

      // Needs to add two extra fields on this farm, i-e EMAIL and PASSWORD, for new login account.
      // Therefore setting two options for accepting extra fields on the farm
      $this->validatorSchema->setOption('allow_extra_fields', true);
      $this->validatorSchema->setOption('filter_extra_fields', false);


      // Assigning proper names to labels
      $this->widgetSchema->setLabels(array(
          'firstname'       =>  'First Name',
          'lastname'        =>  'Last Name',
          'emailaddress'    =>  'Email',
          'university'      =>  'Institute',
          'contact'         =>  'Phone',
          'birthdate'         =>  'Date of Birth',
          'entitytyperefid' =>  'User Type',
          'password_check'  =>  'Re-enter Password',
      ));

      // Chaning the HTML output format from table (default) to list
      //$this->widgetSchema->setFormFormatterName('list');
      
      $this->widgetSchema->setNameFormat('entity[%s]');      
  }
}
